package com.tentact.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Dbutil {        //jdbc:oracle:thin:@192.168.102.200:1521:ORCL
    private static final String username = "root";
    private static final String password = "root";
    private static final String URL = "jdbc:mysql://192.168.102.177:3306/hyyd?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";

    // 加载驱动类
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 获取连接
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL, username, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }

    // 关闭资源
    public static void close(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(Connection conn, PreparedStatement psmt) {
        try {
            psmt.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 关闭资源
    public static void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
        try {
            rs.close();
            psmt.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 增删改通用
    public static int executeUpdate(String sql, Object[] paras) {
        int result = -1;
        Connection conn = getConnection();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    psmt.setObject(i + 1, paras[i]);
                }
            }
            result = psmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close(conn, psmt);
        }
        return result;
    }

    // 查询的通用方法
    public static <T> List<T> executeQuery(String sql, Object[] paras, RowMapper<T> mapper) {
        Connection conn = getConnection();
        PreparedStatement psmt = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<>();
        try {
            psmt = conn.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    psmt.setObject(i + 1, paras[i]);
                }
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                T object = mapper.rowMapper(rs);
                list.add(object);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close(conn, psmt, rs);
        }
        return list;
    }

    // 查询的通用方法（返回值一个对象）
    public static <T> T executeQuerySingle(String sql, Object[] paras, RowMapper<T> mapper) {
        T object = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        Connection conn = getConnection();
        try {
            psmt = conn.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    psmt.setObject(i + 1, paras[i]);
                }
            }
            rs = psmt.executeQuery();
            if (rs.next()) {
                object = mapper.rowMapper(rs);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close(conn, psmt, rs);
        }
        return object;
    }
}
